$(document).ready(function(){
	//输入提示语
	var promptText = {
		realname : {
			normal : "",
			formatError: "真实姓名长度在2到10汉字之间",
			notChinese : "真实姓名必须是汉字",
			isNull : "请输入真实姓名"
		},
		cardnumber : {
			normal : "",
			formatError : "身份证号码格式错误",
			under18 : "必须年满18周岁才可购彩",
			isNull : "请输入身份证号码"
		},
		zipcode : {
			normal : "",
			wrong : "请输入正确的邮政编码"
		},
		mobile : {
			normal : "",
			formatError : "手机号码格式错误",
			isNull : "请输入手机号码"
		},
		email : {
			normal : "",
			formatError : "电子邮箱格式错误",
			isNull : "请输入电子邮箱",
			isExist : "注册邮箱已经存在"
		},
		smscode : {
			normal : "",
			isNull : "请输入验证码"
		}
	};
	
	/**
	 * 获取已经填写并且不可变的信息，undefined表示未填写
	 */
	var editRealName = $("#IrealName[type=hidden]").val();
	var editCardNumber = $("#IcardNumber[type=hidden]").val();
	
	/**
	 * 初始化修改用户资料页面
	 */
	var loadBaseinfo = new Helper.RemoteTrans({
		url: Helper.basePath + "/getMemberinfo.htm",
		isAsync : false,
		onSuccess: function(result) {
			var memb = result.returnObject;
			if('' != memb.userName)
			{
				$("#IuserName").attr("readonly","readonly"); 
				
			}
			$("#IuserName").val(memb.userName == null?"":memb.userName);
			
			if(''!= memb.realName && null!=memb.realName)
			{ 
				$("#IrealName").attr("readonly","readonly");  
				$('#IrealNameDiv').addClass("focus");
				$("#IrealNameVal").val(memb.realName);
			}
		
			$("#IrealName").val(memb.realName == null?"":memb.realName.replace(memb.realName.substr(1,memb.realName.length),"**"));
			if('' != memb.cardNumber && null!=memb.cardNumber)
			{
				$("#IcardNumber").attr("readonly","readonly");
				$('#IcardNumberDiv').addClass("focus");
				$("#IcardNumberVal").val(memb.cardNumber);
			}
			$("#IcardNumber").val(memb.cardNumber == null?"":memb.cardNumber.replace(memb.cardNumber.substr(4,memb.cardNumber.length-8),"*******"));
			$("#Imobile").val(memb.mobile == null?"":memb.mobile);
			$("#Iemail").val(memb.email == null?"":memb.email);
		}
	});
	loadBaseinfo.send();
	
	/**
	 * 修改用户基本资料
	 */
	var updateBaseinfo = new Helper.RemoteTrans( {
		url: Helper.basePath + "/member_operate/doUpdateBaseinfo.htm",
		onSuccess: function(result){
			Helper.message("修改资料成功");
//			loadBaseinfo.send();
			
			if($("#3338login").val()=="3338"){
				window.location.href = Helper.basePath + "/page/3338web/memberInfo/memberIndex.jsp";
			}else{
				window.location.href = Helper.basePath + "/page/memberinfo/";
			}
			
			
		},
		onError: function(page, status){
			Helper.message("修改资料失败");
		}
	});
	
	/**
	 * 检查真实姓名
	 */
	var checkRealName = function(){
		var text = $("#IrealName");
		var value = text.val();
		var flag = false;
		var errorMsg = "";
		if(!Helper.isEmpty(value)){
			if(Helper.validata.isChinese(value)) {
				var len = Helper.validata.getByteLength(value);
				if(len < 4 || len > 20){
					errorMsg = promptText.realname.formatError;
				}else {
					flag = true;
				}
			}else {
				errorMsg = promptText.realname.notChinese;
			}
		}else {
			errorMsg = promptText.realname.isNull;
		}
		if(flag) {
		}else {
			Helper.message(errorMsg);
		}
		return flag;
	};
	
	/**
	 * 身份证号码
	 * 430528198709127392
	 */
	var checkCardNumber = function() {
		var text = $("#IcardNumber");
		var value = text.val();
		var flag = false;
		var errorMsg = "";
		if(!Helper.isEmpty(value)){
			if(Helper.validata.isIdCardNo(value)) {
				if(Helper.validata.suiTableAge(value)) {
					flag = true;
				}else {
					errorMsg = promptText.cardnumber.under18;
				}
			}else {
				errorMsg = promptText.cardnumber.formatError;
			}
		}
		else 
		{
			errorMsg = promptText.cardnumber.isNull;
		}
		
		if(flag) {
				
			}else {
				Helper.message(errorMsg);
			}
		return flag;
	};
	
	/**
	 * 手机校验
	 * 15219876543
	 */
	var checkMobile = function() {
		var text = $("#Imobile");
		var value = text.val();
		var flag = false;
		var errorMsg = "";
		if(!Helper.isEmpty(value)){
			flag = Helper.validata.isMobile(value);
			if(flag) {
			}else {
				errorMsg = promptText.mobile.formatError;
			}
		}else {
			errorMsg = promptText.mobile.isNull;
		}
		if(flag) {
			
		}else {
			Helper.message(errorMsg);
		}
		return flag;
	};
	
	/**
	 * 邮箱校验
	 */
	var checkEmail = function() {
		var text = $("#Iemail");
		var value = text.val();
		var flag = false;
		var errorMsg = "";
		if(!Helper.isEmpty(value)){
			flag = Helper.validata.isEmail(value);
			if(flag) {
			}else {
				errorMsg = promptText.email.formatError;
			}
		}else {
			errorMsg = promptText.email.isNull;
		}
		if(flag) {
			
		}else {
			Helper.message(errorMsg);
		}
		return flag;
	};
	
	$("#submitForm").click(function(){
		  var naVal =$("#IrealNameVal").val();
		  var cardVal =$("#IcardNumberVal").val();
			if(''!= naVal && null!=naVal && ''!=cardVal && null!=cardVal )
			{ 
				if(checkMobile() && checkEmail()) {
					Helper.confirm("你确认要修改当前资料？", function(){
						if(!editRealName)
						{
							updateBaseinfo.addParameters("realName", $("#IrealName").val());
						}
						if(!editCardNumber)
						{
							updateBaseinfo.addParameters("cardNumber", $("#IcardNumber").val());
						}
						updateBaseinfo.addParameters("email", $("#Iemail").val());
						updateBaseinfo.addParameters("mobile", $("#Imobile").val());
						updateBaseinfo.send();
					});
				}
			}else{
				if( checkRealName() && checkCardNumber() && checkMobile() && checkEmail()) {
					Helper.confirm("你确认要修改当前资料？", function(){
						updateBaseinfo.addParameters("realName", $("#IrealName").val());
						updateBaseinfo.addParameters("cardNumber", $("#IcardNumber").val());
						updateBaseinfo.addParameters("email", $("#Iemail").val());
						updateBaseinfo.addParameters("mobile", $("#Imobile").val());
						updateBaseinfo.send();
					});
				}
				
			}
			
	});
});